package manager.cms.service.exec;
import common.ImageBase64Utils;
import common.SnowflakeIdWorker;
import manager.brand.dao.BrandCategoryRlsDao;
import manager.brand.dao.BrandDao;
import manager.brand.vo.BrandCategoryRlsVO;
import manager.brand.vo.BrandVO;
import manager.cms.service.JdExecutor;
import manager.cms.service.utils.JdInternetWormUtils;
import manager.feign.GoodsListThirdService;
import manager.fileupload.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 获取优惠卷信息
 */
@Service(value = "brandExec")
public class BrandExecImpl implements JdExecutor {
    @Autowired
    private GoodsListThirdService goodsListThirdService;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private BrandCategoryRlsDao brandCategoryRlsDao;
    public void execute(Map<String,Object> mapParams) {
        try {
            List<String> categoryIds=(List<String>)mapParams.get("categoryIds");
            String[] categoryStrIds = new String[categoryIds.size()];
            categoryIds.toArray(categoryStrIds);
            String  itemListHtml=goodsListThirdService.getGoodsList(categoryStrIds,1);
            String pattern = "\"J_valueList v-fixed\">[\\S\\s]*?</ul>";
            List<String> paramsStrList= JdInternetWormUtils.getStrArrayByReg(itemListHtml,pattern);
            //进行非贪婪匹配
            String patternLi = "<li id=[\\S\\s]*?</li>";
            List<String> brandStrList= JdInternetWormUtils.getStrArrayByReg(paramsStrList.get(0),patternLi);
            String patternTitle="title=[\\S\\s]*?\">";
            String patternImage="src=\"[\\S\\s]*?g\"";
            List<BrandVO> brandVOS=new ArrayList<BrandVO>();
            brandStrList.forEach(item->{
                 List<String> brandNameList= JdInternetWormUtils.getStrArrayByReg(item,patternTitle);
                 String brandName=brandNameList.get(0);
                 BrandVO brandVO=new BrandVO();
                 //设置品牌名称
                 brandVO.setName(brandName.substring(7,brandName.length()-2));
                 //设置品牌Letter
                 brandVO.setLetter(brandName.substring(0,1));
                 //设置品牌图片
                 List<String> imageList= JdInternetWormUtils.getStrArrayByReg(item,patternImage);
                try {
                    if(imageList.size()>0){
                        String imageUrl="https:"+imageList.get(0).substring(5,imageList.get(0).length()-1);
                        String image= ImageBase64Utils.base64Img(imageUrl,"2",fileUploadService);
                        brandVO.setImage(image);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                brandVOS.add(brandVO);
            });
            List<BrandVO> brandDbVOList=brandDao.selectAll();
            List<BrandVO> addBrandList=new ArrayList<BrandVO>();
            List<BrandCategoryRlsVO> brandCategoryRlsList=new ArrayList<BrandCategoryRlsVO>();
            brandVOS.forEach(item->{
                 List<BrandVO> curList=brandDbVOList.stream().filter(curItem->curItem.getName().equals(item.getName())).collect(Collectors.toList());
                 if(curList.size()==0){
                     String imgId=String.valueOf(SnowflakeIdWorker.nextId());
                     item.setId(imgId);
                     addBrandList.add(item);
                 }
                 BrandCategoryRlsVO brandCategoryRlsVO=new BrandCategoryRlsVO();
                 brandCategoryRlsVO.setBrandId(item.getId());
                 String categoryId=(String)mapParams.get("categoryId");
                 brandCategoryRlsVO.setCategoryId(categoryId);
                 brandCategoryRlsList.add(brandCategoryRlsVO);
            });
            brandDao.insertList(addBrandList);
            brandCategoryRlsDao.insertList(brandCategoryRlsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
